<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_interface/upload.php';

$db = new DB('vip');

$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$rank = (isset($_POST['rank']) && intval($_POST['rank'])==1) ? 1 : 2;
$status_type = isset($_POST['status_type']) ? intval($_POST['status_type']) : 0;
$username = isset($_POST['username']) ? h($_POST['username']) : '';
$name = isset($_POST['name']) ? h($_POST['name']) : '';
$sex = (isset($_POST['sex']) && intval($_POST['sex'])==2) ? 2 : 1;
$postcode = isset($_POST['postcode']) ? h($_POST['postcode']) : '';
$mailaddress = isset($_POST['mailaddress']) ? h($_POST['mailaddress']) : '';
$accountaddress = isset($_POST['accountaddress']) ? h($_POST['accountaddress']) : '';
$vocational = isset($_POST['vocational']) ? h($_POST['vocational']) : '';
$position = isset($_POST['position']) ? h($_POST['position']) : '';
$homephone = isset($_POST['homephone']) ? h($_POST['homephone']) : '';
$weixin = isset($_POST['weixin']) ? h($_POST['weixin']) : '';
$qq = isset($_POST['qq']) ? h($_POST['qq']) : '';
$email = isset($_POST['email']) ? h($_POST['email']) : '';
$telephone = isset($_POST['telephone']) ? h($_POST['telephone']) : '';
$fax = isset($_POST['fax']) ? h($_POST['fax']) : '';
$mobile = isset($_POST['mobile']) ? h($_POST['mobile']) : '';
$cardnumber = isset($_POST['cardnumber']) ? h($_POST['cardnumber']) : '';
$bankaccounttype = isset($_POST['bankaccounttype']) ? intval($_POST['bankaccounttype']) : 0;
$bankaccount = isset($_POST['bankaccount']) ? h($_POST['bankaccount']) : '';
$reservebankaccount = isset($_POST['reservebankaccount']) ? h($_POST['reservebankaccount']) : '';
$reservenote = isset($_POST['reservenote']) ? h($_POST['reservenote']) : '';
$updated_uid = Sessions::get('uid');
$updated = time();
$groupid = isset($_POST['groupid']) ? intval($_POST['groupid']) : 0;
$valid_date = isset($_POST['valid_date']) ? strtotime(h($_POST['valid_date'])) : 0;
$status = isset($_POST['status']) ? intval($_POST['status']) : 1;
$remark = isset($_POST['remark']) ? h($_POST['remark']) : '';
$thread_url = isset($_POST['thread_url']) ? h($_POST['thread_url']) : '';
$old_sfz_zm = isset($_POST['sfz_zm']) ? h($_POST['sfz_zm']) : '';
$old_sfz_fm = isset($_POST['sfz_fm']) ? h($_POST['sfz_fm']) : '';
$old_ghd = isset($_POST['ghd']) ? h($_POST['ghd']) : '';
$old_scsfz = isset($_POST['scsfz']) ? h($_POST['scsfz']) : '';

//检查必填数据
if( $id <= 0 || $username == '' || $name == '' || $sex == '' || $postcode == '' || $mailaddress == '' || $mobile == '' || 
	$cardnumber == '' || $bankaccounttype <= 0 || $bankaccount == '' || $status <= 1 || $groupid < 1 ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

//检查是否重复认证
$vip_to_salesman_sql = 'select status,created from vip_to_salesman where username="'.$username.'" limit 1';
$vip_to_salesman_rs = $db->query($vip_to_salesman_sql)->getOne();
echo $vip_to_salesman_rs['status'];
if( $vip_to_salesman_rs['status'] < 1 ){
	exit;
}

if( $status == 3 ){
	$db->table('vip_to_salesman')->where('id='.$id)->update(array('status'=>3))->run();//更新
	echo '<script>alert("拒绝成功！");window.history.go(-1);</script>';
	exit;
}

//拼扩展用户组id
$my_extgroupids = '';
$extgroupids = '';
if( isset($_POST['extgroupids']) && is_array($_POST['extgroupids']) ){
	foreach( $_POST['extgroupids'] as $v ){
		$my_extgroupids .= $v."\t";
		$extgroupids .= $v."<-->";
	}
}

//交易组有效期
if( $rank == 1 ){
	if( $valid_date == 0 ){
		$valid_date = strtotime('+1 year');
	}
}else{
	$valid_date = 0;
}

//组装vip的update数组
$vip_to_salesman_data=array('name'=>$name,
					'sex'=>$sex,
					'postcode'=>$postcode,
					'mailaddress'=>$mailaddress,
					'accountaddress'=>$accountaddress,
					'vocational'=>$vocational,
					'position'=>$position,
					'homephone'=>$homephone,
					'weixin'=>$weixin,
					'qq'=>$qq,
					'email'=>$email,
					'telephone'=>$telephone,
					'fax'=>$fax,
					'mobile'=>$mobile,
					'cardnumber'=>$cardnumber,
					'bankaccounttype'=>$bankaccounttype,
					'bankaccount'=>$bankaccount,
					'reservebankaccount'=>$reservebankaccount,
					'reservenote'=>$reservenote,
					'updated_uid'=>$updated_uid,
					'updated'=>$updated,
					'verify_time'=>$updated,
					'status'=>$status,
					'remark'=>$remark,
					'thread_url'=>$thread_url,
					'rank'=>$rank,
					'groupid'=>$groupid,
					'valid_date'=>$valid_date,
					'extgroupids'=>$extgroupids,
			);

$up = new Upload($_FILES,'../../../vip_upload/','jpg|png|gif',2048);     //创建
$fs = $up->execute();    //开始执行，并返回数据
//身份证正面
if( isset($_FILES['attach']['size']['sfz_zm']) && $_FILES['attach']['size']['sfz_zm'] > 0){
	if($fs['sfz_zm']['flag'] == -2){
		echo '<script>alert("上传图片大于2M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['sfz_zm']['flag'] == 1){
		$sfz_zm = $fs['sfz_zm']['handle_dir'].$fs['sfz_zm']['name'];
		$vip_to_salesman_data['sfz_zm'] = $sfz_zm;
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{
	$vip_to_salesman_data['sfz_zm'] = $old_sfz_zm;
}
//身份证反面
if( isset($_FILES['attach']['size']['sfz_fm']) && $_FILES['attach']['size']['sfz_fm'] > 0){
	if($fs['sfz_fm']['flag'] == -2){
		echo '<script>alert("上传图片大于2M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['sfz_fm']['flag'] == 1){
		$sfz_fm = $fs['sfz_fm']['handle_dir'].$fs['sfz_fm']['name'];
		$vip_to_salesman_data['sfz_fm'] = $sfz_fm;
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{
	$vip_to_salesman_data['sfz_fm'] = $old_sfz_fm;
}
//固话单图片
if( isset($_FILES['attach']['size']['ghd']) && $_FILES['attach']['size']['ghd'] > 0){
	if($fs['ghd']['flag'] == -2){
		echo '<script>alert("上传图片大于2M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['ghd']['flag'] == 1){
		$ghd = $fs['ghd']['handle_dir'].$fs['ghd']['name'];
		$vip_to_salesman_data['ghd'] = $ghd;
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{
	$vip_to_salesman_data['ghd'] = $old_ghd;
}
//上传手持身份证的照片
if( isset($_FILES['attach']['size']['scsfz']) && $_FILES['attach']['size']['scsfz'] > 0){
	if($fs['scsfz']['flag'] == -2){
		echo '<script>alert("上传图片大于2M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['scsfz']['flag'] == 1){
		$scsfz = $fs['scsfz']['handle_dir'].$fs['scsfz']['name'];
		$vip_to_salesman_data['scsfz'] = $scsfz;
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{
	$vip_to_salesman_data['scsfz'] = $old_scsfz;
}
$db->table('vip_to_salesman')->where('id='.$id)->update($vip_to_salesman_data)->run();//更新

$old_vip_sql = 'select id from vip_info where username="'.$username.'" and status=2 limit 1';
$old_vip_rs = $db->query($old_vip_sql)->getOne();
if( $old_vip_rs['id'] > 0 ){
	$db->table('vip_info')->where('username="'.$username.'"')->update($vip_to_salesman_data)->run();//更新
}else{
	$vip_to_salesman_data['username'] = $username;
	$vip_to_salesman_data['created'] = $vip_to_salesman_rs['created'];
	$db->table('vip_info')->insert($vip_to_salesman_data)->run();//插入
}

//同步修改论坛用户组
$my_member_group_url = 'http://bbs.zgqw.com/s_api/edit_member_group_vip.php';
$my_username = urlencode(htmlspecialchars($username));
if( $status == 2 ){//认证通过
	$my_groupid = $groupid;
}elseif( $status == 3 ){
	$my_groupid = 10;
}
$my_valid_date = $valid_date;
$my_extgroupids = urlencode(htmlspecialchars($my_extgroupids));
$member_group_context = array();
$member_group_context['http'] = array(
	'method' => 'POST',
	'content' => 'pw=zgqw_2013&username='.$my_username.'&groupid='.$my_groupid.'&extgroupids='.
				$my_extgroupids.'&valid_date='.$my_valid_date.'&verify=1&status='.$status,
	'header'=>"Content-type: application/x-www-form-urlencoded;"."\r\n"."Referer:http://vip.zgqw.com"."\r\n".'User-Agent : vvm'."\r\n"."X-FORWARDED-FOR:1.1.1.9"."\r\n"."CLIENT-IP:2.2.2.7",
);
@file_get_contents($my_member_group_url, false, stream_context_create($member_group_context));

//同步签名到论坛
$my_signature_url = 'http://bbs.zgqw.com/s_api/edit_signature_vip.php';
$signature = '';
if( $status == 2 ){//认证通过
	if( $valid_date != 0 ){
		$signature .= '认证员注:交易级别有效期至'.date('Y年m月d日',$valid_date)."[vip_sign]";
	}
	$signature .= '姓名:'.$name."(已通过身份核查，号码一致，照片一致)[vip_sign]";
	if( $mailaddress != '' ){
		$signature .= '地址:'.$mailaddress."； ";
	}
	if( $postcode != '' ){
		$signature .= '邮编:'.$postcode."[vip_sign]";
	}
	if( $homephone != '' ){
		$signature .= '电话:'.$homephone."； ";
	}
	if( $mobile != '' ){
		$signature .= '手机:'.$mobile."[vip_sign]";
	}
	if( $weixin != '' && $qq != '' ){
		$signature .= '微信号:'.$weixin."； QQ:".$qq."[vip_sign]";
	}else{
		if( $weixin != '' ){
			$signature .= '微信号:'.$weixin."[vip_sign]";
		}
		if( $qq != '' ){
			$signature .= 'QQ:'.$qq."[vip_sign]";
		}
	}
	$bankarray = array( '1'=>'邮政', '2'=>'农行', '3'=>'工行', '4'=>'建行', '5'=>'交行', '6'=>'招行', '7'=>'中行', '8'=>'浦发', '9'=>'民生', );
	if( $bankaccounttype != 0 && $bankaccount != '' ){
		$signature .= $bankarray[$bankaccounttype].':'.$bankaccount."[vip_sign]";
	}
	if( $reservebankaccount != '' ){
		$signature .= '其他银行:'.$reservebankaccount."[vip_sign]";
	}
	if( $remark != '' ){
		$signature .= '管理员注:'.$remark."[vip_sign]";
	}
}
$my_signature = urlencode(htmlspecialchars($signature));
$signature_context = array();
$signature_context['http'] = array(
	'method' => 'POST',
	'content' => 'pw=zgqw_2013&username='.$my_username.'&signature='.$my_signature,
	'header'=>"Content-type: application/x-www-form-urlencoded;"."\r\n"."Referer:http://vip.zgqw.com"."\r\n".'User-Agent : vnm'."\r\n"."X-FORWARDED-FOR:1.1.8.1"."\r\n"."CLIENT-IP:2.2.5.2",
);
@file_get_contents($my_signature_url, false, stream_context_create($signature_context));

echo '<script>alert("保存成功!");window.location.href="/admin/vip/vip_lists_upgrade.php?status_type='.$status_type.'";</script>';
exit;